//
//  WeightUnitPickerViewController.m
//  IWeighMyself
//
//  Created by Francisco Gindre on 8/5/10.
//  Copyright (c) 2010 __MyCompanyName__. All rights reserved.
//

#import "WeightUnitPickerViewController.h"
#import "IWeighMyselfAppDelegate.h"

@implementation WeightUnitPickerViewController
@synthesize weightPickerView;
@synthesize newUserView;

#pragma mark -
#pragma mark Constants

#define PICKER_ROW_HEIGHT 40
#define PICKER_COMPONENTS 1
#define PICKER_WIDTH 240


#pragma mark -
#pragma mark constructor

-(id)initWithParentView:(NewUserViewController*)parentView {
	self = [[WeightUnitPickerViewController alloc]init];
	newUserView = parentView;
	[self.newUserView retain];
	
	return self;
}

#pragma mark -
#pragma mark view life cycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.weightPickerView = [[UIPickerView alloc]initWithFrame:CGRectZero];
	
	[self setTitle:NSLocalizedString(@"WeightUnitPickerTitle",@"")];
	
	
	/*
	 create done button
	 */
	
	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:NSLocalizedString(@"DoneButtonTitle",@"") 
																			 style:UIBarButtonItemStyleDone 
																			target:self 
																			action:@selector(doneButtonAction:)];
	[self createPicker];
	
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark UIPickerViewDelegate



/*
 Called by the picker view when the user selects a row in a component.
 */
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	// report the selection
	//NSLog(@"Selectedrow didSelectRow %@",[weightPickerViewArray objectAtIndex:[pickerView selectedRowInComponent:0]]);
}
#pragma mark -
#pragma mark UIPickerViewDataSource

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{		
	return [weightPickerViewArray objectAtIndex:row];

}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
	
	return PICKER_WIDTH;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
	return PICKER_ROW_HEIGHT;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	return [weightPickerViewArray count];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return PICKER_COMPONENTS;
}

#pragma mark -
#pragma mark UIPickerView

/*
 return the picker frame based on its size, positioned at the bottom of the page
 */
- (CGRect)pickerFrameWithSize:(CGSize)size
{
	CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
	CGRect pickerRect = CGRectMake(	0.0,
								   screenRect.size.height - 120 - size.height,
								   size.width,
								   size.height);
	return pickerRect;
}

- (void)createPicker
{
	weightPickerViewArray = [[NSArray arrayWithObjects:
						NSLocalizedString(@"WeightUnitKilos",@""),
						NSLocalizedString(@"WeightUnitPounds",@""),
						nil] retain];
	// note we are using CGRectZero for the dimensions of our picker view,
	// this is because picker views have a built in optimum size,
	// you just need to set the correct origin in your view.
	//
	// position the picker at the bottom
	weightPickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
	CGSize pickerSize = [weightPickerView sizeThatFits:CGSizeZero];
	weightPickerView.frame = [self pickerFrameWithSize:pickerSize];
	
	weightPickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
	weightPickerView.showsSelectionIndicator = YES;	// note this is default to NO
	
	// this view controller is the data source and delegate
	weightPickerView.delegate = self;
	weightPickerView.dataSource = self;
	
//	// add this picker to our view controller, initially hidden
//		weightPickerView.hidden = YES;
	[self.view addSubview:weightPickerView];
}


#pragma mark -
#pragma mark button actions
/*
 performs the "Done" action setting the weight unit in newUserViewController
 */

-(void)doneButtonAction:(id)sender {
	
	
	[[newUserView.cellValues objectAtIndex:newUserView.weightUnitIndexPath.section]
													replaceObjectAtIndex:newUserView.weightUnitIndexPath.row 
																				 withObject:[weightPickerViewArray objectAtIndex:[weightPickerView selectedRowInComponent:0]]] ;
	
	
	/*
	 set weight into the plistxml
	 */
	
	NSString *weight = [weightPickerViewArray objectAtIndex:[weightPickerView selectedRowInComponent:0]];
	NSLog(@"weight unit is %@",weight);
	[newUserView.appDelegate.plistXML setObject:weight forKey:@"WeightUnit"];
	
	
	/*
	 set the weight flag to yes so the user does not get a wrong pop up
	 */
	
	newUserView.isWeightUnitSet = YES;
	
		
	/*
	 dismiss controller
	 */
	
	[self.navigationController popViewControllerAnimated:YES];
	
	
}

@end
